<?php

/**
 * University form.
 *
 * @package    Amman to London
 * @subpackage form
 * @author     Adel Abu-Saif <adeljas@msn.com>
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class UniversityForm extends BaseUniversityForm {

    public function configure() {

        $objs = $this->getObject()->getFaculty();

        $posted_faculties = $this->count_post_faculties();

        $this->widgetSchema['name_en']->setLabel('Name [ English ]');
        $this->widgetSchema['address_en']->setLabel('Address [ English ]');
        $this->widgetSchema['name_ar']->setLabel('Name [ Arabic ]');
        $this->widgetSchema['address_ar']->setLabel('Address [ Arabic ]');

        if ($objs instanceof Doctrine_collection && count($objs) > 0) {
            foreach ($objs as $k => $one) {
                $form = new FacultyForm($one);
                unset($form['course_list']);
                unset($form['university_id']);
                $this->embedForm('faculty_' . ($k + 1), $form);
            }

            $faculties = count($objs);
            if ($posted_faculties > $faculties) {
                while ($posted_faculties != $faculties) {
                    $k++;
                    $faculties++;
                    $form = new FacultyForm();
                    unset($form['course_list']);
                    unset($form['university_id']);
                    $this->embedForm('faculty_' . ($k + 1), $form);
                }
            }
        } else {

            if (sfContext::getInstance()->getRequest()->getMethod() == 'POST') {
                $post_params = sfContext::getInstance()->getRequest()->getPostParameters();
                $c = 1;
                foreach ($post_params['university'] as $k => $one) {
                    if (strpos($k, 'faculty') !== false) {
                        $form = new FacultyForm();
                        unset($form['course_list']);
                        unset($form['university_id']);
                        $this->embedForm('faculty_' . ($c), $form);
                        $c++;
                    }
                }
            } else {
                $form = new FacultyForm();
                unset($form['course_list']);
                unset($form['university_id']);
                $this->embedForm('faculty_1', $form);
            }
        }
    }

    private function count_post_faculties() {

        $post_params = sfContext::getInstance()->getRequest()->getPostParameters();
        if (is_array($post_params) && count($post_params)) {
            $c = 0;
            foreach ($post_params['university'] as $k => $one) {
                if (strpos($k, 'faculty') !== false) {
                    $c++;
                }
            }
            return $c;
        }
        return false;
    }


    public function saveEmbeddedForms($con = null, $forms = null) {
        if (null === $con) {
            $con = $this->getConnection();
        }

        if (null === $forms) {
            $forms = $this->embeddedForms;
        }

        foreach ($forms as $form) {
            if ($form instanceof sfFormObject) {
                $form->saveEmbeddedForms($con);
                $obj = $form->getObject();
                if ($form instanceof FacultyForm)
                    $obj->setUniversityId($this->getObject()->getId());
                $form->getObject()->save($con);
            } else {
                $this->saveEmbeddedForms($con, $form->getEmbeddedForms());
            }
        }
    }

}
